import { GTileList } from "../../../../com/mui/controls/GTileList";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { PackItem } from "./PackItem";
import { DragEvent } from "../../../../com/mui/events/DragEvent";
import { DataEvent } from "../../events/DataEvent";
import { ItemFeatureTips } from "./social/ItemFeatureTips";
import { MsgManager } from "../../manager/MsgManager";
import { Language } from "../../../../extend/language/Language";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { ItemData } from "../../resource/info/ItemData";
import { BaseItem } from "../common/BaseItem";
import { EPlayerItemPosType } from "../../../../Message/Public/EPlayerItemPosType";
import { GameManager } from "../../manager/GameManager";
import { ModuleType } from "../common/ModuleType";
import { DragDropData } from "./DragDropData";
type int = number;
//class PackItemPanel
    
    export  class PackItemPanel extends GTileList
    {
        constructor()
        {
            super();
            this.initTile();
            Dispatcher.addEventListener(EventName.ButtomBtn_BackPack_Splite, this.buttomBtn_BackPack_Splite);
            Dispatcher.addEventListener(EventName.ButtomBtn_BackPack_Sale, this.buttomBtn_BackPack_Sale);
            return;
        }

        public get posType(): int
        {
            return this._posType;
        }

        public set posType(arg1: int)
        {
            this._posType = arg1;
            return;
        }

        /* internal  */initTile(): void
        {
            this.setSize(283, 246);
            this.contentWidth = 224;
            this.rowHeight = 36;
            this.columnWidth = 36;
            this.horizontalGap = 5;
            this.verticalGap = 5;
            this.setStyle("skin", new Object /* flash.display.Bitmap */());
            this.setStyle("cellRenderer", PackItem);
            this.addEventListener(DragEvent.Event_Move_In, this.moveInBagHandler);
            return;
        }

        /* internal  */buttomBtn_BackPack_Splite(arg1: DataEvent): void
        {
            var loc1=PackItem(this.activeCellRenderers[this.selectedIndex]);
            if (loc1) 
            {
                ItemFeatureTips.splitTip(loc1.itemData);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(40073));
            }
            return;
        }

        /* internal  */buttomBtn_BackPack_Sale(arg1: DataEvent): void
        {
            if (this.selectedIndex < 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(40074));
            }
            var loc1=PackItem(this.activeCellRenderers[this.selectedIndex]);
            if (loc1 && loc1.itemData) 
            {
                if (ItemsUtil.isTaskItem(loc1.itemData)) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(40075));
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopSellItem, loc1.itemData.uid));
            }
            return;
        }

        public updateAllItem(arg1: DataProvider, arg2: Boolean=true): void
        {
            this.dataProvider = arg1;
            return;
        }

        /* internal  */EqualItemData(arg1: ItemData, arg2: ItemData): Boolean
        {
            if (arg1 && arg2 && arg1.uid == arg2.uid && arg1.itemAmount == arg2.itemAmount) 
            {
                return true;
            }
            return false;
        }

        /* internal  */moveInBagHandler(arg1: DragEvent): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            if (this._dropable) 
            {
                loc1 = arg1.dragItem as BaseItem;
                loc2 = arg1.dropItem as BaseItem;
                loc3 = arg1.dragSouce as ItemData;
                loc4 = loc2.itemData;
                if (loc1 == loc2) 
                {
                    return;
                }
                if (loc3 && !(loc3.posType == EPlayerItemPosType._EPlayerItemPosTypeStoneBag) && this._posType == EPlayerItemPosType._EPlayerItemPosTypeStoneBag) 
                {
                    return;
                }
                if (loc3 && !(loc3.posType == EPlayerItemPosType._EPlayerItemPosTypeAmuletBag) && this._posType == EPlayerItemPosType._EPlayerItemPosTypeAmuletBag) 
                {
                    return;
                }
                if (loc1.posType == EPlayerItemPosType._EPlayerItemPosTypeWarehouseExtendBar) 
                {
                    return;
                }
                if (!loc2.data["isOpened"]) 
                {
                    GameManager.instance.popupWindow(ModuleType.WareHouseBuyItem);
                    return;
                }
                if (loc3 && (this._posType == EPlayerItemPosType._EPlayerItemPosTypeBag || this._posType == loc3.posType)) 
                {
                    loc5 = new DragDropData(loc3.posType, this._posType, loc3.uid, loc1.posIndex, loc2.posIndex, loc3, loc4);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_DragInItem, loc5));
                }
            }
            arg1.stopImmediatePropagation();
            return;
        }

        /* internal  */throwGoodsHandler(arg1: DragEvent): void
        {
            ItemFeatureTips.destroyItem((arg1.dragItem as PackItem).itemData);
            return;
        }

        public updataSelectIndex(arg1: int): void
        {
            this.selectedIndex = arg1;
            return;
        }

        public selectItem(): PackItem
        {
            return PackItem(this.activeCellRenderers[this.selectedIndex]);
        }

        public getPackItemByCode(arg1: int): PackItem
        {
            var loc2=null;
            var loc1=0;
            while (loc1 < this.length) 
            {
                loc2 = this.activeCellRenderers[loc1] as PackItem;
                if (loc2) 
                {
                    if (loc2.itemData.itemCode == arg1) 
                    {
                        return loc2;
                    }
                }
                ++loc1;
            }
            return null;
        }

        public getPackItemByIndex(arg1: int): Boolean
        {
            var loc1=getItemAt(arg1);
            if (loc1) 
            {
                return true;
            }
            return false;
        }

        public get dragable(): Boolean
        {
            return this._dragable;
        }

        public set dragable(arg1: Boolean)
        {
            var loc2=null;
            this._dragable = arg1;
            var loc1=0;
            while (loc1 < this.numChildren) 
            {
                loc2 = this.getChildAt(loc1);
                if (typeof loc2 === "baseitem") 
                {
                    (loc2 as BaseItem).isDragAble = this._dragable;
                }
                ++loc1;
            }
            return;
        }

        public get dropable(): Boolean
        {
            return this._dropable;
        }

        public set dropable(arg1: Boolean)
        {
            this._dropable = arg1;
            return;
        }

        public static /* const */nullData: Object={};

        /* internal */ /* var */_posType: int=0;

        /* internal */ /* var */_dragable: Boolean=true;

        /* internal */ /* var */_dropable: Boolean=true;
    }
